/*
 * @lc app=leetcode.cn id=19 lang=java
 *
 * [19] 删除链表的倒数第 N 个结点
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */
class Solution {
    public ListNode removeNthFromEnd(ListNode head, int n) {
        ListNode beforeHead = new ListNode(0);
        beforeHead.next = head;
        ListNode firstNode = beforeHead;
        ListNode secordNode = beforeHead;
        for ( int size = 0 ; size < n ; size++){
            secordNode = secordNode.next;
        }
        while (secordNode.next != null){
            secordNode = secordNode.next;
            firstNode = firstNode.next;
        }
        ListNode tempHead = firstNode.next;
        firstNode.next = tempHead.next;
        return beforeHead.next;
    }
}
// @lc code=end

